﻿using DongFangGuoKai.WMS.IApiService;
using DongFangGuoKai.WMSPlatform.Core;
using DongFangGuoKai.WMSPlatform.Domain.AGVModels;
using DongFangGuoKai.WMSPlatform.Domain.Enums;
using DongFangGuoKai.WMSPlatform.Domain.Models;
using Microsoft.AspNetCore.Mvc;
using System.Transactions;

namespace DongFangGuoKai.WMSPlatform.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class SchedulingTaskController : ControllerBase
    {
        private IAGVService aGVService { get; }
        private Repository<t_Instruct> _instructRepo { get; }
        private Repository<t_Inventory> _inventoryRepo { get; }
        private Repository<t_PutawayOrder> _putwayOrderRepo { get; }


        public SchedulingTaskController(IAGVService aGVService, Repository<t_Instruct> instructRepo)
        {
            this.aGVService = aGVService;
            _instructRepo = instructRepo;

        }


        /// <summary>
        /// 添加任务单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("AddTask")]
        public async Task<IActionResult> AddTask()
        {

            t_Instruct instruct;
            using TransactionScope ts = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);

            try
            {
                //保存wms指令
                instruct = new t_Instruct()
                {
                    WorkDeviceType = EmWorkDeviceType.AGV,
                    InstructType = EmInstructType.In,
                    Action = EmDeviceAction.Transport,
                    DeviceCode = "",
                    InstructIndex = 1,
                    Priority = 9999,
                    FinishedQTY = 0,
                    StartTime = DateTime.Now,
                    BusinessGroupId = Guid.NewGuid(),
                    BusinessType = (int)EmBusinessType.In,
                    LogicalCellAddr = ""  //仓位编码
                };

                bool result = await _instructRepo.InsertAsync(instruct);

                //保存库存表

                //保存上架单主表


                ts.Complete();

            }
            catch (Exception ex)
            {
                ts.Dispose();

                throw;
            }



            var res = PointToPoint(instruct.BusinessGroupId);

            return new JsonResult(res);
        }

        /// <summary>
        /// 容器与仓位绑定，解绑
        /// </summary>
        [HttpPost]
        [Route("BindCntrAndBin")]
        public IActionResult BindCntrAndBin()
        {
            BindCtnrAndBinModel bindCtnrAndBinModel = new BindCtnrAndBinModel();

            var uuid = Guid.NewGuid();
            bindCtnrAndBinModel.reqCode = uuid.ToString("N");
            bindCtnrAndBinModel.taskCode = bindCtnrAndBinModel.reqCode;
            bindCtnrAndBinModel.reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            //绑定用户
            bindCtnrAndBinModel.clientCode = "PDA";
            //绑定容器类型 大容器类型
            bindCtnrAndBinModel.ctnrTyp = "3";
            //banging起始目标储位  
            bindCtnrAndBinModel.stgBinCode = "T00001T1501013";
            //绑定
            bindCtnrAndBinModel.indBind = "1";

            var res = aGVService.BindCntrAndUnBindTask(bindCtnrAndBinModel);

            return new JsonResult(res);
        }

        /// <summary>
        /// 点对点测试
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        private ResultRespone PointToPoint(Guid guid)
        {
            //添加任务单
            AddTaskModel addTaskModel = new AddTaskModel();


            //生成32位uuid
            addTaskModel.reqCode = guid.ToString("N");
            addTaskModel.clientCode = "PDA";
            //大料箱任务类型
            addTaskModel.TaskTyp = "A11";

            //大容器类型
            addTaskModel.CtnrTyp = "2";
            //生成36位uuid
            addTaskModel.taskCode = guid.ToString();

            //任务起始点仓位编码
            addTaskModel.PositionCodePath = new PositionCodePathDto[2] {
                    new PositionCodePathDto() {
                        PositionCode="D00080A2502013",// 起始地址 D00080A2502013
                        Type="05"
                    },
                    new PositionCodePathDto() {
                        PositionCode="DR0001B2502013",//ERP目标地址
                        Type="05"
                    }
                };

            var res = aGVService.CreateTask(addTaskModel);

            return res;
        }

        /// <summary>
        /// 从货架出库到输送线
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        private ResultRespone HuoJiaToOutShuSongXiang(string guid)
        {
            //添加任务单
            AddTaskModel addTaskModel = new AddTaskModel();


            addTaskModel.reqCode = guid;
            addTaskModel.clientCode = "PDA";
            //输送线出库
            addTaskModel.TaskTyp = "B5";

            //大容器类型
            addTaskModel.CtnrTyp = "2";
            addTaskModel.taskCode = guid;
            addTaskModel.TaskMode = "1";

            //任务起始点仓位编码
            addTaskModel.PositionCodePath = new PositionCodePathDto[2] {
                    new PositionCodePathDto() {
                        PositionCode="D00081A2502013",// 起始地址 D00080A2502013
                        Type="05"
                    },
                    //实际传入编号，测试传入仓位
                    new PositionCodePathDto() {
                        PositionCode="D00080A2502013",//ERP目标地址
                        Type="05"
                    }
                };

            var res = aGVService.CreateTask(addTaskModel);

            return res;
        }

        /// <summary>
        /// 从输送线空箱入货架
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        private ResultRespone ShuSongXianToHuoJia(string guid)
        {
            //添加任务单
            AddTaskModel addTaskModel = new AddTaskModel();


            addTaskModel.reqCode = guid;
            addTaskModel.clientCode = "PDA";
            //输送线入库
            addTaskModel.TaskTyp = "B4";

            //大容器类型
            addTaskModel.CtnrTyp = "2";
            addTaskModel.taskCode = guid;


            //任务起始点仓位编码
            addTaskModel.PositionCodePath = new PositionCodePathDto[2] {
                    new PositionCodePathDto() {
                        //测试阶段为仓位， 真实情况需要修改
                        PositionCode="D00080A2502013",// 起始地址 D00080A2502013
                        Type="05"
                    },
                    //实际传入编号，测试传入仓位
                    new PositionCodePathDto() {
                        PositionCode="D00081A2502013",//ERP目标地址
                        Type="05"
                    }
                };

            var res = aGVService.CreateTask(addTaskModel);
            return res;
        }

        /// <summary>
        /// ARM入库
        /// </summary>
        /// <returns></returns>
        private ResultRespone ChaCheToRuKu(string guid)
        {
            //添加任务单
            AddTaskModel addTaskModel = new AddTaskModel();
            addTaskModel.reqCode = guid;
            addTaskModel.clientCode = "PDA";
            //ARM入库出库
            addTaskModel.TaskTyp = "B1";

            //叉车对应的大容器类型
            addTaskModel.CtnrTyp = "3";
            addTaskModel.taskCode = guid;


            //任务起始点仓位编码
            addTaskModel.PositionCodePath = new PositionCodePathDto[2] {
                    new PositionCodePathDto() {
                        //叉车实际位置
                        PositionCode="O1",// 起始地址 D00080A2502013
                        Type="00"
                    },
                    //实际传入编号，测试传入仓位
                    new PositionCodePathDto() {
                        PositionCode="T00001T1501013",//ERP目标地址
                        Type="05"
                    }
                };

            var res = aGVService.CreateTask(addTaskModel);
            return res;
        }


        /// <summary>
        /// ARM出库
        /// </summary>
        /// <returns></returns>
        private ResultRespone ChaCheToChuKu(string guid)
        {
            //添加任务单
            AddTaskModel addTaskModel = new AddTaskModel();
            addTaskModel.reqCode = guid;
            addTaskModel.clientCode = "PDA";
            //ARM入库出库
            addTaskModel.TaskTyp = "B1";

            //叉车对应的大容器类型
            addTaskModel.CtnrTyp = "3";
            addTaskModel.taskCode = guid;


            //任务起始点仓位编码
            addTaskModel.PositionCodePath = new PositionCodePathDto[2] {
                    new PositionCodePathDto() {
                        //叉车实际位置
                        PositionCode="T00001T1501013",// 起始地址 D00080A2502013
                        Type="05"
                    },
                    //实际传入编号，测试传入仓位
                    new PositionCodePathDto() {
                        PositionCode="O1",//ERP目标地址
                        Type="00"
                    }
                };

            var res = aGVService.CreateTask(addTaskModel);
            return res;
        }

        /// <summary>
        /// 输送线入播种墙2
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        private ResultRespone SHuSongXianToBo(string guid)
        {

            //添加任务单
            AddTaskModel addTaskModel = new AddTaskModel();
            addTaskModel.reqCode = guid;
            addTaskModel.clientCode = "PDA";
            //输送线入库
            addTaskModel.TaskTyp = "B4";

            //大容器类型
            addTaskModel.CtnrTyp = "2";
            addTaskModel.taskCode = guid;


            //任务起始点仓位编码
            addTaskModel.PositionCodePath = new PositionCodePathDto[2] {
                    new PositionCodePathDto() {
                        //测试阶段为仓位， 真实情况需要修改
                        PositionCode="D00080A2502013",// 起始地址 D00080A2502013
                        Type="05"
                    },
                    //实际传入编号，测试传入仓位
                    new PositionCodePathDto() {
                        PositionCode="DR0001B2502013",//ERP目标地址
                        Type="05"
                    }
                };

            var res = aGVService.CreateTask(addTaskModel);
            return res;
        }

        /// <summary>
        /// 播种墙入货架2
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        private ResultRespone BoZhongQiangToHuoJia(string guid)
        {
            //添加任务单
            AddTaskModel addTaskModel = new AddTaskModel();
            addTaskModel.reqCode = guid;
            addTaskModel.clientCode = "PDA";
            //输送线入库
            addTaskModel.TaskTyp = "A11";

            //大容器类型
            addTaskModel.CtnrTyp = "2";
            addTaskModel.taskCode = guid;


            //任务起始点仓位编码
            addTaskModel.PositionCodePath = new PositionCodePathDto[2] {
                    new PositionCodePathDto() {
                        //测试阶段为仓位， 真实情况需要修改
                        PositionCode="DR0001B2502013",// 起始地址 D00080A2502013
                        Type="05"
                    },
                    //实际传入编号，测试传入仓位
                    new PositionCodePathDto() {
                        PositionCode="D00081A2502013",//ERP目标地址
                        Type="05"
                    }
                };

            var res = aGVService.CreateTask(addTaskModel);
            return res;
        }

        /// <summary>
        /// 继续执行任务
        /// </summary>
        [HttpPost]
        [Route("AgvContiune")]
        [Obsolete("停用")]
        public IActionResult AgvContiune(ContinueTaskModel test)
        {
            string guid = Guid.NewGuid().ToString("N");
            ContinueTaskModel continueTaskModel = new ContinueTaskModel();
            //设备编号
            continueTaskModel.agvCode = "";
            continueTaskModel.reqCode = guid;
            continueTaskModel.taskCode = "f4445845b6a64c6599365f2b98f476de";
            continueTaskModel.clientCode = "PDA";


            continueTaskModel.reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            continueTaskModel.nextPositionCode = new Nextpositioncode()
            {
                positionCode = "D00081A2502013",
                type = "05"
            };


            var res = aGVService.ContinueRunTask(continueTaskModel);

            return new JsonResult(res);
        }

        /// <summary>
        /// 申请继续执行
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("AgvBoxApplyPass")]
        public IActionResult AgvBoxApplyPass()
        {
            BoxApplyPassModel boxApplyPassModel = new BoxApplyPassModel();
            boxApplyPassModel.taskCode = "317a01e609654da88a426d47c4eba4fe";
            boxApplyPassModel.reqCode = Guid.NewGuid().ToString("N");
            boxApplyPassModel.reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //取是1，放是2 
            boxApplyPassModel.type = "1";
            var res = aGVService.BoxApplyPass(boxApplyPassModel);

            return new JsonResult(res);
        }

    }
}
